<script>
var viewId = '{$view.id}';
var view = {$view|json_encode};
var filters = {$filters|json_encode};
var actveFilter = {$activeFilter|json_encode};
var default_recs_on_page = 50;
	
{if $view.add_action}
	function add(id) {
		window.location = '{$view.add_action.url}?aid={$view.add_action.id}&vid={$view.id}{if $fk!==false}&fk={$fk}{/if}&referer={$smarty.server.REQUEST_URI|escape:url}';
	}
{/if}
{if $view.edit_action}
	function edit(id) {
		window.location = '{$view.edit_action.url}?aid={$view.edit_action.id}&vid={$view.id}&k[{$view.primary_key_field}]='+id+'&referer={$smarty.server.REQUEST_URI|escape:url}';
	}
{/if}

$(function(){
	grid = new Grid(
		$('#{$view.id}'),
		'{$view.id}', 
		{ 
			selectableRows: true,
			recs_on_page: view.records_on_page,
			{if $fk!==false} fk: { '{$view.foreign_key_field}':'{$fk}' }, {/if} 
			filter: actveFilter ? actveFilter.fields : {}
			{if $view.edit_action} ,toolbar: [
				{ 'icon':'ui-icon-pencil', 'action': function(id){ edit(id); }, 'title':'Редактировать' }
			] {/if}
		} 
	);
	{if $view.edit_action}
		grid.onDblClick = function(id) { edit(id); }
	{/if}
	grid.onSelect = function(id) {
		$('#actionsOnMultiple').toggle(this.getSelected().length!=0);
	}
	grid.onSelectAll = function() {
		$('#actionsOnMultiple').toggle(this.getSelected().length!=0);
	}
});
</script>
<script type="text/javascript" src="js/list.js"></script>

<div id='filters' class='toolbar ui-widget ui-widget-content ui-corner-all'>
	<div><span class='ui-icon ui-icon-triangle-1-s'></span></div>
	<div class='edit'>
		<div class='tabs'>
			<ul>
				<li class='new-filter'><a href='#new-filter'><span class="ui-icon ui-icon-plus" role="presentation">Новый фильтр</span></a></li>
			</ul>
			<div id='new-filter'></div>
		</div>
		<div class='tools'>
			<button class='find' title="Найти">Найти</button>
			<button class='cancel' title="Отменить">Отменить</button>			
		</div>
	</div>
	<div class='buttons'></div>
</div>

<div id='filter_templates' style="display:none;">
	<div class='filter_tools ui-widget-header ui-corner-all'>
		<ul class='filter_fields'>
			{foreach $view.allfields as $ff}
				<li data-name='{$ff.name}' data-type='{$ff.type_id}' data-title='{$ff.title}'><span class="ui-icon ui-icon-check"></span>{$ff.title}</li>
			{/foreach}
		</ul>
		<button class='add_field'>Добавить поле</button>
		<button class='save'>Сохранить</button>
	</div>
	<table>
		<tr data-type='string'>
			<td></td>
			<td>
				<select name='type'>
					<option value='like'>Содержит</option>
					<option value='='>Равно</option>
					<option value='<>'>Не равно</option>
				</select>
			</td>
			<td>
				<input type=text name='value' class='str'/>
			</td>
		</tr>
		<tr data-type='integer'>
			<td></td>
			<td>
				<select name='type'>
					<option value='='>Равно</option>
					<option value='<>'>Не равно</option>
					<option value='>'>Больше</option>
					<option value='>='>Больше или равно</option>
					<option value='<'>Меньше</option>
					<option value='<='>Меньше или равно</option>
				</select>
			</td>
			<td>
				<input type=text name='value' class='int'/>
			</td>
		</tr>
		<tr data-type='bool'>
			<td></td>
			<td>
				<select name='type'>
					<option value='on'>Да</option>
					<option value='off'>Нет</option>
				</select>
			</td>
			<td>
			</td>
		</tr>
	</table>
	<div id='save_filter_dialog' title="Сохранить фильтр">
		<p>
			Название фильтра: <input type=text name='name' size=20 maxlength=64 />
		</p>
	</div>
</div>


<div class="toolbar ui-widget-header ui-corner-all" style="display:hidden;">
{if $view.del_action}
	<span id='actionsOnMultiple' style="display:none;">
		<button id='del' onclick="del()" title='Удалить'>Удалить</button>
	</span>
{/if}
{if $view.add_action}
	<button id='add' onclick="add()" title="Добавить">Добавить</button>
{/if}
	<button id='refresh'>Обновить</button>
	
<span style="padding-left:20px;">
{if $view.add_action}
	<button id='do_import'>Импорт</button>
{/if}
</span>

<button id='settings'>Настройки</button>

</div>

<div id='list_settings' title="Настройки просмотра" data-init=0>
	<table>
		<tr>
			<td>
			</td>
			<td>
				Выбранные колонки:
			</td>
			<td>
			</td>
			<td>
				Доступные колонки:
			</td>
		</tr>
		<tr>
			<td>
				<button class='up'>Выше</button>
				<button class='down'>Ниже</button>
			</td>
			<td class='visible'>
				<select size=10 class="ui-corner-all">
				</select>
			</td>
			<td>
				<button class='add'>Добавить</button>
				<button class='del'>Удалить</button>
			</td>			
			<td class='other'>
				<select size=10 class="ui-corner-all">
				</select>
			</td>
		</tr>
		<tr>
			<td colspan=4>
				<table>
					<tr>
						<td>
							Сортировка по полю:
						</td>
						<td>
							<select class='sort_field'>
								<option value=''>&nbsp;- нет -&nbsp;
							</select> 
							<select class='sort_dir'>
								<option value='0'>По возрастанию
								<option value='1'>По убыванию
							</select>
						</td>
					</tr>
				</table>
			</td>
		</tr>
		<tr>
			<td colspan=4>
				<table>
					<tr>
						<td>
							Записей на странице:
						</td>
						<td>
							<select class='records_on_page'>
								<option value=20>20</option>
								<option value=50>50</option>
								<option value=100>100</option>
								<option value=200>200</option>
								<option value=500>500</option>
								<option value=1000000000>Все</option>
							</select>
						</td>
					</tr>
				</table>
			</td>
	</table>
</div>

<div id='{$view.id}'></div>